package com.accessibleworlds.user;

import com.accessibleworlds.gateway.Login;
import com.accessibleworlds.gateway.SignUp;
import com.accessibleworlds.util.SharedVariables;
import com.accessibleworlds.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;

public class StartPage extends Activity implements OnClickListener{
		
	private Button signUp,login,guest,exit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED)
		{
		    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		}
        
		setContentView(R.layout.startpage);
		
        initializeVariables();
        changeLanguage();
	}

	@Override
	protected void onResume() {
		super.onResume();
        changeLanguage();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
	
	private void initializeVariables() {
		signUp = (Button) findViewById(R.id.startPageSignUp);
		login = (Button) findViewById(R.id.startPageLogin);
		guest = (Button) findViewById(R.id.startPageGuest);
		exit = (Button) findViewById(R.id.startPageExit);
		
		signUp.setOnClickListener(this);
		login.setOnClickListener(this);
		guest.setOnClickListener(this);
		exit.setOnClickListener(this);
	}
	
	private void changeLanguage() {
		signUp.setText(getResources().getStringArray(R.array.signUp)[SharedVariables.currentLanguage]);
		login.setText(getResources().getStringArray(R.array.login)[SharedVariables.currentLanguage]);
		guest.setText(getResources().getStringArray(R.array.guest)[SharedVariables.currentLanguage]);
		exit.setText(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage]);
	}
	
	public void onClick(View v) {
		if(v.getId()==signUp.getId()) {
			Intent intent = new Intent(this,SignUp.class);
			Bundle bundle = new Bundle();
			bundle.putString("previousID", SharedVariables.startPageID);
			intent.putExtras(bundle);
			startActivity(intent);
			overridePendingTransition(R.anim.fade, R.anim.hold);
			finish();
		}
		else if(v.getId()==login.getId()) {
			Intent intent = new Intent(this,Login.class);
			Bundle bundle = new Bundle();
			bundle.putString("previousID", SharedVariables.startPageID);
			intent.putExtras(bundle);
			startActivity(intent);
			overridePendingTransition(R.anim.fade, R.anim.hold);
			finish();
		}
		else if(v.getId()==guest.getId()) {
			Intent intent = new Intent(this,GuestUserPage.class);
			Bundle bundle = new Bundle();
			bundle.putString("previousID", SharedVariables.startPageID);
			intent.putExtras(bundle);
			startActivity(intent);
			overridePendingTransition(R.anim.fade, R.anim.hold);
			finish();
		}
		else if(v.getId()==exit.getId()) {
			finish();
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater mainMenu = getMenuInflater();
		mainMenu.inflate(R.menu.mainmenu, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.findItem((R.id.menuOptions)).setTitle(getResources().getStringArray(R.array.options)[SharedVariables.currentLanguage]);
		menu.findItem(R.id.menuOptions).setIcon(R.drawable.optionsicon);
		menu.findItem((R.id.menuExit)).setTitle(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage]);
		menu.findItem((R.id.menuExit)).setIcon(R.drawable.exiticon);
		return super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId()==R.id.menuOptions) {
			Intent intent = new Intent(this,Options.class);
			Bundle bundle = new Bundle();
			bundle.putString("previousID", SharedVariables.startPageID);
			intent.putExtras(bundle);
			startActivity(intent);
			overridePendingTransition(R.anim.fade, R.anim.hold);
		}
		else if (item.getItemId()==R.id.menuExit) {
			finish();
		}
		return super.onOptionsItemSelected(item);
	}
}
